import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 晓星航
 * Date: 2023-11-28
 * Time: 10:21
 */
public class ResourceAllocation2 {
    public static void main(String[] args) {
        // 工厂数量
        int m = 3;

        // 机器数量
        int n = 5;

        // 每个工厂在拥有不同机器数量时的盈利值
        int[][] profits = {
                {0, 3, 7, 9, 12, 13},
                {0, 5, 10, 11, 11, 11},
                {0, 4, 6, 11, 12, 12}
        };

        // 分配方案
        int[] allocation = new int[m];

        // 遍历每台机器
        for (int i = 0; i < n; i++) {
            // 找到能够带来最大盈利的工厂
            int maxProfit = 0;
            int maxFactory = -1;

            for (int j = 0; j < m; j++) {
                int[] tempAllocation = allocation.clone();
                tempAllocation[j]++;

                // 计算当前分配方案的总盈利值
                int profit = calculateTotalProfit(tempAllocation, profits);

                if (profit > maxProfit) {
                    maxProfit = profit;
                    maxFactory = j;
                }
            }

            // 更新分配方案
            allocation[maxFactory]++;
        }

        // 输出结果
        System.out.println("最大总盈利：" + calculateTotalProfit(allocation, profits));
        System.out.println("资源分配方案：" + Arrays.toString(allocation));
    }

    // 计算给定资源分配方案的总盈利
    public static int calculateTotalProfit(int[] allocation, int[][] profits) {
        int totalProfit = 0;

        for (int i = 0; i < allocation.length; i++) {
            totalProfit += profits[i][allocation[i]];
        }

        return totalProfit;
    }
}
